package ye;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;

public class HttpClientHelper {

    static Logger logger = Logger.getLogger("HttpClientHelper");

    /**
     * @param strUrl
     * @return String
     */
    public static String doGet(String strUrl) {
        String receive = null;

        // 请求发布在本地 Tomcat上服务
        GetMethod method = new GetMethod(strUrl);
        try {
            HttpClient client = new HttpClient();
            // 设置为默认的重试策略
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            //设置编码
            method.getParams().setContentCharset("UTF-8");
            // 5秒超时
            method.getParams().setSoTimeout(30000);

            String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
            method.setRequestHeader("User-Agent", userAgent);

            int rspCode = client.executeMethod(method);
            //System.out.println("rspCode:" + rspCode);

            InputStream inputStream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer result = new StringBuffer();
            String str = "";
            while ((str = br.readLine()) != null) {
                result.append(str);
            }

            return result.toString();
        } catch (Exception e) {
            //e.printStackTrace();
            //logger.info(strUrl + " Exception:" + e.getMessage());
            logger.info("url:" + strUrl + "," + e.getMessage());
            receive = e.getMessage();
        } finally {
            method.releaseConnection();
        }
        return receive;
    }

    /**
     * @param strUrl
     * @param postString
     * @return String
     */
    @SuppressWarnings("deprecation")
    public static String doPost(String strUrl, String postString) {
        String receive = null;

        // 请求发布在本地 Tomcat上服务
        PostMethod method = new PostMethod(strUrl);
        try {
            HttpClient client = new HttpClient();

            method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
            method.setRequestHeader("Accept", "application/json; charset=UTF-8");

            // 设置为默认的重试策略
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            // 5秒超时
            method.getParams().setSoTimeout(5000);
            method.setRequestBody(postString);

            int rspCode = client.executeMethod(method);
            //System.out.println("rspCode:" + rspCode);

            InputStream inputStream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer result = new StringBuffer();
            String str = "";
            while ((str = br.readLine()) != null) {
                result.append(str);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
        return receive;
    }
}